package cn.tedu.file;
/**
 * @Author 金帮嘉诚  @Date 2021-06-17 周四 14:38
 */
/*本类用于测试字节输出流*/

import java.io.*;

/**
 * 知识点：
 * OutputStream()
 * FileOutputStream() 两个参数，第二个true时为追加数据，第二个为false或者不设置时为覆盖数据
 * BufferedOutputStream()
 */
public class TestOut {
    public static void main(String[] args) {
        method1();//普通字节输出流
        method2();
//        method3();
//        method4();
    }

    private static void method4() {
        OutputStream out =null;

        try {
//            out=
//                    new BufferedOutputStream
//                            (new FileOutputStream
//                                    (new File("D:\\ready\\2.txt")
//                                    )
//                            );
            out=new BufferedOutputStream
                    (new FileOutputStream("D:\\ready\\2.txt"));

            out.write(99);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method3() {
        OutputStream out=null;
        try {
//            out=new FileOutputStream(new File("D:\\ready\\1.txt"));
            out = new FileOutputStream("D:\\ready\\1.txt");
            out.write(100);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method2() {//先写结构

        OutputStream out = null;
        try {
//            out =
//            new BufferedOutputStream
//            (new FileOutputStream
//            (new File("D:\\ready\\1.txt")));
            out = new BufferedOutputStream(new FileOutputStream("D:\\ready\\2.txt",true));

            out.write(101);
            out.write(102);
            out.write(103);
            out.write(104);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method1() {
        OutputStream out = null;
//        out=new FileOutputStream(new File("D:\\ready\\1.txt"));
        try {
//            out = new FileOutputStream("D:\\ready\\2.txt");
            out = new FileOutputStream("D:\\ready\\2.txt",true);//是否追加

            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);
            out.write(100);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
